/**
 * Created by jerome.arnoult on 15/05/14.
 */
function gamerenderer(idCanvas, stage){
    this.stage = stage;
    this.intervalRender;//used only in horrible case of outdated browser
    this.stopRendering = true;
    this.optimizeRender(idCanvas, stage);
    this.toggleFPS = false;
    this.FPS;
    this.useTicker = true;
}
//Perform optim
gamerenderer.prototype.optimizeRender = function(idCanvas, stage){
    var canvas = document.getElementById(idCanvas);
    canvas.width = (window.innerWidth) >>0;
    canvas.style.width = window.innerWidth + 'px';
    canvas.height = (window.innerHeight) >>0;
    canvas.style.height = window.innerHeight + 'px';
}
//Queueing frame
gamerenderer.prototype.queueNewFrame = function(){
    if (window.requestAnimationFrame)
        window.requestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.msRequestAnimationFrame)
        window.msRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.webkitRequestAnimationFrame)
        window.webkitRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.mozRequestAnimationFrame)
        window.mozRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.oRequestAnimationFrame)
        window.oRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else {
        game.gameRenderer.queueNewFrame = function () {};
        intervalRender = window.setInterval(game.gameRenderer.renderingLoop, 16.7);
    }
}
//Render loop
gamerenderer.prototype.renderingLoop = function(){
    if(this.useTicker){
        createjs.Ticker.setFPS(60);
        createjs.Ticker.addEventListener("tick",function(e){
            if(!e.paused){
                //game.gameMap.update();
                game.stage.update();
                if(game.gameRenderer.toggleFPS === true)
                    game.gameRenderer.FPS = createjs.Ticker.getMeasuredFPS();
            }
        });
    }else{
        if(!game.gameRenderer.stopRendering){
            //game.gameMap.update();
            game.stage.update();
        }
        game.gameRenderer.queueNewFrame();
    }
}